#define _CRT_SECURE_NO_WARNINGS
#include <thread>
#include <mutex>
#include <condition_variable>
#include <iostream>






using namespace std;


int main()
{
	mutex mtx;
	condition_variable cond;
	bool flag = true;
	int num = 100;


	thread t1([&]() 
		{
			int i = 0;

			while (i < num)
			{
				unique_lock<mutex> lmtx(mtx);

				while (!flag)
				{
					cond.wait(lmtx);
				}

				cout << i << endl;
				i += 2;

				flag = false;
				cond.notify_one();
			}
		});

	thread t2([&]()
		{
			int j = 1;

			while (j < num)
			{
				unique_lock<mutex> lmtx(mtx);

				while (flag)
				{
					cond.wait(lmtx);
				}

				cout << j << endl;
				j += 2;

				flag = true;
				cond.notify_one();
			}
		});


	t1.join();
	t2.join();


	return 0;
}